package com.csaba.db;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.csaba.util.LoggerHelper;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin
{
	public static final String IMG_REFRESH = "refresh.gif";
	public static final String IMG_OPEN_DB = "open_db.gif";
	public static final String IMG_CLOSED_DB = "closed_db.gif";
	public static final String IMG_ADD_DB = "add_db.gif";
	public static final String IMG_CLOSE = "close_user.GIF";
	public static final String IMG_OPEN = "open_user.GIF";

	private final static Logger logger = LoggerHelper.getDefaultLogger();

	// The plug-in ID
	public static final String PLUGIN_ID = "com.csaba.db";

	// The shared instance
	private static Activator plugin;

	/**
	 * The constructor
	 */
	public Activator()
	{
		LoggerHelper.initLogger();
	}

	/**
	 * On starting up the plugin all open projects (databases) should be closed. Project can be opened only
	 * on entering the password.
	 */
	@Override
	public void start( final BundleContext context ) throws Exception
	{
		super.start(context);
		plugin = this;

		DatabaseManager.getInstance().closeAllConnections();
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	@Override
	public void stop( final BundleContext context ) throws Exception
	{
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault()
	{
		return plugin;
	}

	@Override
	protected void initializeImageRegistry( final ImageRegistry registry )
	{
		registerImage(registry, IMG_ADD_DB);
		registerImage(registry, IMG_CLOSED_DB);
		registerImage(registry, IMG_OPEN_DB);
		registerImage(registry, IMG_REFRESH);

		super.initializeImageRegistry(registry);
	}

	private void registerImage( final ImageRegistry registry, final String key )
	{
		try
		{
			final ImageDescriptor desc = getImageDescriptor("./icons/" + key);
			registry.put(key, desc);
		}
		catch ( final Exception e )
		{
			logger.log(Level.SEVERE, "Error loading image descriptor.", e);
		}
	}

	/**
		 * Returns an image descriptor for the image file at the given
		 * plug-in relative path
		 *
		 * @param path the path
		 * @return the image descriptor
		 */
	public ImageDescriptor getImageDescriptor( final String path )
	{
		final ImageDescriptor desc = getImageRegistry().getDescriptor(path);
		if ( desc == null )
			return imageDescriptorFromPlugin(PLUGIN_ID, "./icons/" + path);
		return desc;
	}

	public static void throwCoreException( final String message ) throws CoreException
	{
		final IStatus status = new Status(IStatus.ERROR, "com.csaba.db", IStatus.OK, message, null);
		throw new CoreException(status);
	}

	public static void throwCoreException( final String message, final Exception cause ) throws CoreException
	{
		final IStatus status = new Status(IStatus.ERROR, "com.csaba.db", IStatus.OK, message, cause);
		throw new CoreException(status);
	}

}
